package String;



public class _387_FirstUniqueCharacterinaString {
    //hint:use hashMap array can solve the problem
    public int firstUniqChar(String s) {
        char[] a = new char[26];
        int[] count = new int[26];
        int[] position = new int[26];
        int pos = 0;
        int length = s.length();
        for(int i=0;i<length;i++) {
            boolean check = false;
            char temp = s.charAt(i);
            for(int j=0;j<pos;j++) {
                if (a[j] == temp) {
                    count[j]++;
                    check = true;
                    break;
                }
            }
            if (!check) {
                position[pos] = i;
                a[pos] = temp;
                count[pos++] = 1;
            }
        }
        for(int i=0;i<count.length;i++) {
            if (count[i] == 1) {
                return position[i];
            }
        }
        return -1;
    }

    //hint:circle from the string,not the char array
    public int firstUniqChar1(String s) {
        int[] freq = new int[26];
        for(int i=0;i<s.length();i++) {
            freq[s.charAt(i)-'a']++;
        }
        for(int i=0;i<s.length();i++) {
            if (freq[s.charAt(i) - 'a'] == 1) {
                return i;
            }
        }
        return -1;
    }
}
